package dao

import (
	"mldong/internal/modules/{{.ModuleName}}/entity"
	"mldong/utility"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"{{ if .HasTime }}
	"github.com/gogf/gf/v2/os/gtime"{{end}}
)

type {{.TableCamelName}}Dao struct {
}

func (u *{{.TableCamelName}}Dao) Insert(entity entity.{{.TableCamelName}}, tx gdb.TX) (res entity.{{.TableCamelName}}, _ error) {
	{{ if .HasTime }}var now = gtime.Now(){{end}}{{ if .HasCreateTime }}
	entity.CreateTime = now{{end}}{{ if .HasUpdateTime }}
	entity.UpdateTime = now{{end}}
	if entity.ID == nil {
		entity.ID = utility.GenId()
	}
	var _, err = u.Model(tx).Data(entity).Insert()
	return entity, err
}

func (u *{{.TableCamelName}}Dao) UpdateById(entity entity.{{.TableCamelName}}, tx gdb.TX) (count int64, _ error) {
	{{ if .HasTime }}var now = gtime.Now(){{end}}{{ if .HasUpdateTime }}
	entity.UpdateTime = now{{end}}
	var result, err = u.Model(tx).Data(entity).Where("id=?", entity.ID).Update()
	count, _ = result.RowsAffected()
	return count, err
}

func (u *{{.TableCamelName}}Dao) DeleteById(id string, tx gdb.TX) (count int64, _ error) {
    {{ if .HasIsDeleted }}var now = gtime.Now()
    var result, err = u.Model(tx).Data(g.Map{"is_deleted": 1, "update_time": now}).Where("id=?", id).Update(){{ else }}
    var result, err = u.Model(tx).Where("id=?", id).Delete(){{ end }}
    if err != nil {
        return 0, err
    }
    count, _ = result.RowsAffected()
    return count, nil
}

func (u *{{.TableCamelName}}Dao) DeleteByIds(ids []string) (count int64, _ error) {
    {{ if .HasIsDeleted }}var now = gtime.Now()
    var result, err = g.Model("{{.RealTableName}}").Data(g.Map{"is_deleted": 1, "update_time": now}).WhereIn("id", ids).Update(){{ else }}
    var result, err = g.Model("{{.RealTableName}}").WhereIn("id", ids).Delete(){{ end }}
    if err != nil {
        return 0, err
    }
    count, _ = result.RowsAffected()
    return count, nil
}

func (u *{{.TableCamelName}}Dao) GetById(id string) (res entity.{{.TableCamelName}}, err error) {
	var model = g.Model("{{.RealTableName}}").Where("id=?", id){{ if .HasIsDeleted }}
	model = model.Where("is_deleted=?", 0){{ end }}
	err = model.Scan(&res)
	return res, err
}

func (u *{{.TableCamelName}}Dao) Page(pageNum int, pageSize int, modelHandler func(m *gdb.Model) *gdb.Model) (users []entity.{{.TableCamelName}}, total int, err error) {
	var model = g.Model("{{.RealTableName}} t")
	// 允许外部修改 model
	if modelHandler != nil {
		model = modelHandler(model)
	}{{ if .HasIsDeleted }}
	model = model.Where("t.is_deleted=?", 0){{ end }}
	err = model.Page(pageNum, pageSize).ScanAndCount(&users, &total, false)
	return users, total, err
}

func (u *{{.TableCamelName}}Dao) List(modelHandler func(m *gdb.Model) *gdb.Model) (list []entity.{{.TableCamelName}}, err error) {
	var model = g.Model("{{.RealTableName}} t")
	// 允许外部修改 model
	if modelHandler != nil {
		model = modelHandler(model)
	}{{ if .HasIsDeleted }}
	model = model.Where("t.is_deleted=?", 0){{ end }}
	err = model.Scan(&list)
	return list, err
}

func (u *{{.TableCamelName}}Dao) Model(tx gdb.TX) *gdb.Model {
	if tx != nil {
		return tx.Model("{{.RealTableName}}")
	}
	return g.Model("{{.RealTableName}}")
}

func New{{.TableCamelName}}Dao() *{{.TableCamelName}}Dao {
	return &{{.TableCamelName}}Dao{}
}